﻿using iMES.Core.Extensions;
using iMES.Core.Utilities;
using iMES.Entity.DomainModels;
using iMES.Mass.Repositories;
using iMES.Mass.IRepositories;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iMES.Mass.Services
{
    public partial class Mass_DetectionItemsService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IMass_DetectionItemsRepository mass_DetectionItemsRepository;//访问数据库
        [ActivatorUtilitiesConstructor]
        public Mass_DetectionItemsService(
            IMass_DetectionItemsRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            mass_DetectionItemsRepository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            AddOnExecuting = (Mass_DetectionItems model, object list) =>
            {
                if (string.IsNullOrWhiteSpace(model.Mass_DetectionItems_Code))
                    model.Mass_DetectionItems_Code = GetMass_DetectionItemsCode(model);
                //判断是否有重复的编码
                if (repository.Exists(x => x.Mass_DetectionItems_Code == model.Mass_DetectionItems_Code))
                {
                    return webResponse.Error("检测项编码已存在！");
                }
                //判断是否存在同名
                if (repository.Exists(x => x.Mass_DetectionItems_Name == model.Mass_DetectionItems_Name))
                {
                    return webResponse.Error("检测项名称已存在！");
                }
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
        /// <summary>
        /// 编辑操作
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveModel)
        {
            UpdateOnExecute = (SaveModel model) =>
            {
                return webResponse.OK();
            };
            //编辑方法保存数据库前处理
            UpdateOnExecuting = (Mass_DetectionItems model, object addList, object updateList, List<object> delKeys) =>
            {
                return webResponse.OK();
            };
            return base.Update(saveModel);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="delList"></param>
        /// <returns></returns>
        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            for (int i = 0; i < keys.Length; i++)
            {
                List<object> idsProject = Mass_DetectionItemsRepository.Instance.FindAsIQueryable
                    (x => x.Mass_DetectionItems_ID == keys[i].GetInt()).Select(s => (object)s.Mass_DetectionItems_ID).ToList();
                object[] arrayProject = idsProject.ToArray();
                mass_DetectionItemsRepository.DeleteWithKeys(arrayProject, true);
            }
            //最终保存
            repository.SaveChanges();
            return base.Del(keys, true);
        }
        /// <summary>
        /// 自动生成编码
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string GetMass_DetectionItemsCode(Mass_DetectionItems entity)
        {
            // 若有编码，则直接返回
            if (!string.IsNullOrEmpty(entity.Mass_DetectionItems_Code))
            {
                return entity.Mass_DetectionItems_Code;
            }
            else
            {
                // 自动生成编码
                var list = Mass_DetectionItemsRepository.Instance.FindAsIQueryable
                    (x => x.Mass_DetectionItems_Code.StartsWith("JCX")).OrderByDescending(x => x.Mass_DetectionItems_Code).ToList().FirstOrDefault();
                if (list == null)
                {
                    return "JCX" + DateTime.Now.ToString("yyyyMMdd") + "0001";
                }
                else
                {
                    string code = list.Mass_DetectionItems_Code.Substring(11,4);
                    int num = int.Parse(code) + 1;
                    return "JCX" + DateTime.Now.ToString("yyyyMMdd") + num.ToString().PadLeft(4, '0');
                }
            }
        }
    }
}
